{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Untitled",
      "provenance": [],
      "include_colab_link": true
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    },
    "accelerator": "TPU"
  },
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "view-in-github",
        "colab_type": "text"
      },
      "source": [
        "<a href=\"https://colab.research.google.com/github/yananma/5_programs_per_day/blob/master/1020.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "u4h1iYkZOaJI",
        "colab_type": "text"
      },
      "source": [
        "## 1、 数据的加载和预处理 "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "V8KTiUWeOhh5",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch \n",
        "torch.__version__"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "S1826IgGPGsS",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from torch.utils.data import Dataset \n",
        "import pandas as pd "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "mt_6fI3FTPnF",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "class BulldozerDataset(Dataset):\n",
        "    def __init__(self, csv_file):\n",
        "        self.df = pd.read_csv(csv_file)\n",
        "\n",
        "    def __len__(self):\n",
        "        return len(self.df)\n",
        "\n",
        "    def __getitem__(self, idx):\n",
        "        return self.df.iloc[idx].SalePrice"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WDmYTGawVNKv",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from google.colab import files \n",
        "uploaded = files.upload()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4H3W3G82TuBN",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "ds_demo = BulldozerDataset('median_benchmark.csv')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cAEphDqQUAo5",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "len(ds_demo)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "s1dcYMWlWGGr",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "ds_demo[0]"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HnLxbY9TWKyc",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "ds_demo.df.info()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ONBAxYP5WRwk",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "ds_demo.df.head(15)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2BO0VqRNWep8",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "dl = torch.utils.data.DataLoader(ds_demo, batch_size=10, shuffle=True, num_workers=0)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "iidDVu99XWv9",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "idata = iter(dl)\n",
        "print(idata)\n",
        "print(next(idata))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "aw1om0ZaXhmd",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "for i, data in enumerate(dl):\n",
        "    if i < 6:\n",
        "        print(i, data)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "doBMj0KjX4Vv",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torchvision.datasets as datasets "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GX9Yh0XDYdS-",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "train_set = datasets.MNIST(root='./data', train=True, download=True, transform=None )"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AOIy6Do8YrVl",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torchvision.models as models \n",
        "resnet18 = models.resnet18(pretrained=True)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ox5x5hA1ZMpJ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torchvision.transforms as transforms "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KTk0iJUBaWue",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "transform = transforms.Compose([\n",
        "    transforms.RandomCrop(32, padding=4), \n",
        "    transforms.RandomHorizontalFlip(), \n",
        "    transforms.RandomRotation((-45, 45)),\n",
        "    transforms.ToTensor(), \n",
        "    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))\n",
        "])"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "63y0BQJvbHNm",
        "colab_type": "text"
      },
      "source": [
        "## 2、深度学习基础与数学原理"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6PqyMVSKct-Y",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch \n",
        "from torch.nn import Linear, Module, MSELoss \n",
        "from torch.optim import SGD \n",
        "import numpy as np \n",
        "import pandas as pd \n",
        "import matplotlib.pyplot as plt \n",
        "import seaborn as sns \n",
        "torch.__version__"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3EYV9sF3eC2f",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x = np.linspace(0, 20, 500)\n",
        "y = 5*x + 7 \n",
        "plt.plot(x, y)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "jMJw19ENeNvZ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x = np.random.rand(256)\n",
        "noise = np.random.randn(256) / 4 \n",
        "y = x * 5 + 7 + noise \n",
        "df = pd.DataFrame()\n",
        "df['x'] = x \n",
        "df['y'] = y "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "IiggaYfVep1e",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "sns.lmplot(x='x', y='y', data=df)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fbi-aVgfexYg",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "model = Linear(1, 1)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8n-JVXiLe3lQ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "criterion= MSELoss()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WScrmdUde7_Q",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "optim = SGD(model.parameters(), lr=0.01)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5i2OTX1SfD4u",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "epochs = 3000 "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "YxqZs9FZfF8s",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x_train = x.reshape(-1, 1).astype('float32')\n",
        "y_train = y.reshape(-1, 1).astype('float32')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1iB_qM2FfU-2",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "for i in range(epochs):\n",
        "    inputs = torch.from_numpy(x_train)\n",
        "    labels = torch.from_numpy(y_train)\n",
        "\n",
        "    outputs = model(inputs)\n",
        "    optim.zero_grad()\n",
        "    loss = criterion(outputs, labels)\n",
        "    loss.backward()\n",
        "    optim.step()\n",
        "    if i % 100 == 99:\n",
        "        print('Epoch [{}/{}] Loss: {:1.4f}'.format(i+1, epochs, loss.data.item()))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tC1xSK6IgbSL",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "[w, b] = model.parameters()\n",
        "print(w.item(), b.item())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "KOpvAyKMgwEW",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "predicted = model.forward(torch.from_numpy(x_train)).data.numpy()\n",
        "plt.plot(x_train, y_train, 'bo', label='data', alpha=0.3)\n",
        "plt.plot(x_train, predicted, label='predicted')\n",
        "plt.legend()\n",
        "plt.show()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "vaXsolo9hd04",
        "colab_type": "text"
      },
      "source": [
        "## 3、线性回归(1)"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "njRNxckw4TMe",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch \n",
        "import torch.nn as nn \n",
        "import numpy as np \n",
        "import matplotlib.pyplot as plt "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fUZcnE2F7Rqb",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "input_size = 1 \n",
        "output_size = 1 \n",
        "num_epochs = 60 \n",
        "learning_rate = 0.001 "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "AHNwGgD_7cSH",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x_train = np.array([[3.3], [4.4], [5.5], [6.71], [6.93], [4.168], \n",
        "                    [9.779], [6.182], [7.59], [2.167], [7.042], \n",
        "                    [10.791], [5.313], [7.997], [3.1]], dtype=np.float32)\n",
        "\n",
        "y_train = np.array([[1.7], [2.76], [2.09], [3.19], [1.694], [1.573], \n",
        "                    [3.366], [2.596], [2.53], [1.221], [2.827], \n",
        "                    [3.465], [1.65], [2.904], [1.3]], dtype=np.float32)\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8gZGLiB68MGH",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "model = nn.Linear(input_size, output_size)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "l6FLzb0v8S0X",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "criterion = nn.MSELoss()\n",
        "optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nx60P69l8cS-",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "for epoch in range(num_epochs):\n",
        "    inputs = torch.from_numpy(x_train)\n",
        "    targets = torch.from_numpy(y_train)\n",
        "\n",
        "    outputs = model(inputs)\n",
        "    loss = criterion(outputs, targets)\n",
        "\n",
        "    optimizer.zero_grad()\n",
        "    loss.backward()\n",
        "    optimizer.step()\n",
        "\n",
        "    if (epoch+1) % 5 == 0:\n",
        "        print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7lelJxcl9MeE",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "predicted = model(torch.from_numpy(x_train)).detach().numpy()\n",
        "plt.plot(x_train, y_train, 'ro', label='Original data')\n",
        "plt.plot(x_train, predicted, label='Fitted line')\n",
        "plt.legend()\n",
        "plt.show()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nETaKxmY9mtf",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "torch.save(model.state_dict(), 'model.ckpt')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "1UwILtrh9wEI",
        "colab_type": "text"
      },
      "source": [
        "## 4、线性回归(2)"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "vd2y6PmB9-Jm",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch \n",
        "import torch.nn as nn \n",
        "import numpy as np \n",
        "import matplotlib.pyplot as plt "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pVVEQhlM_1Gg",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# 超参数\n",
        "input_size = 1 \n",
        "output_size = 1 \n",
        "num_epochs = 3000 \n",
        "learning_rate = 0.001 "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "C6-h8v2VAJ_3",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# data\n",
        "x = np.random.rand(256)\n",
        "noise = np.random.randn(256) / 4 \n",
        "y = x * 5 + 7 + noise\n",
        "\n",
        "x_train = x.reshape(-1, 1).astype('float32')\n",
        "y_train = y.reshape(-1, 1).astype('float32')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "FA28gnPtAWnh",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# 模型\n",
        "model = nn.Linear(input_size, output_size)\n",
        "# 分开\n",
        "\n",
        "# Loss and optimizer\n",
        "criterion = nn.MSELoss()\n",
        "optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DlGowD-pAi3B",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# 训练模型\n",
        "for epoch in range(num_epochs):\n",
        "    inputs = torch.from_numpy(x_train)\n",
        "    targets = torch.from_numpy(y_train)\n",
        "    \n",
        "    outputs = model(inputs)\n",
        "    loss = criterion(outputs, targets)\n",
        "\n",
        "    optimizer.zero_grad()\n",
        "    loss.backward()\n",
        "    optimizer.step()\n",
        "\n",
        "    if (epoch+1) % 100 == 0:\n",
        "        print('Epoch [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, loss.item()))\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gYwu6NOuTRqn",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "[w, b] = model.parameters()\n",
        "print(w.item(), b.item())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "LO0-SeYRT_zK",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "predicted = model(torch.from_numpy(x_train)).detach().numpy()\n",
        "plt.plot(x_train, y_train, 'bo', label='Original data', alpha=0.3)\n",
        "plt.plot(x_train, predicted, label='Fitted line')\n",
        "plt.legend()\n",
        "plt.show()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Jrc5SkOrUmfF",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "torch.save(model.state_dict(), 'model.ckpt')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "_ZKWBn9UU4aP",
        "colab_type": "text"
      },
      "source": [
        "## 5、Logistic Regression"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "78OXYpu7VQ6L",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch \n",
        "import torch.nn as nn \n",
        "import torchvision \n",
        "import torchvision.transforms as transforms "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lDplJ1yjKwhU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "input_size = 784 \n",
        "num_classes = 10 \n",
        "num_epochs = 5 \n",
        "batch_size = 100 \n",
        "learning_rate = 0.001 "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "2QdVDXE4K8pV",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "train_dataset = torchvision.datasets.MNIST(root='./data', train=True, transform=transforms.ToTensor(), download=True)\n",
        "test_dataset = torchvision.datasets.MNIST(root='./data', train=False, transform=transforms.ToTensor())\n",
        "\n",
        "train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)\n",
        "test_loader = torch.utils.data.DataLoader(dataset=test_dataset, batch_size=batch_size, shuffle=False)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "BOkIFAvTMOtX",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "model = nn.Linear(input_size, num_classes)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "iYwhLQmPOiMc",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "criterion = nn.CrossEntropyLoss()\n",
        "optimizer = torch.optim.SGD(model.parameters(), lr=learning_rate)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wfjQHmggOwXZ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "total_step = len(train_loader)\n",
        "for epoch in range(num_epochs):\n",
        "    for i, (images, labels) in enumerate(train_loader):\n",
        "        images = images.reshape(-1, 28*28)\n",
        "        outputs = model(images)\n",
        "        loss = criterion(outputs, labels)\n",
        "\n",
        "        optimizer.zero_grad()\n",
        "        loss.backward()\n",
        "        optimizer.step()\n",
        "\n",
        "        if (i+1) % 100 == 0:\n",
        "            print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, i+1, total_step, loss.item()))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "pIUhMLbzPr0G",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "with torch.no_grad():\n",
        "    correct = 0 \n",
        "    total = 0 \n",
        "    for images, labels in test_loader:\n",
        "        images = images.reshape(-1, 28*28)\n",
        "        outputs = model(images)\n",
        "        _, predicted = torch.max(outputs.data, 1)\n",
        "        total += labels.size(0)\n",
        "        correct += (predicted == labels).sum()\n",
        "\n",
        "    print('Accuracy: {}%'.format(100 * correct / total))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "j7W9hNAyQkc2",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "torch.save(model.state_dict(), 'model.ckpt')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "FQbvmicZQrd2",
        "colab_type": "text"
      },
      "source": [
        "## 6、PyTorch basics"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "udoVcCjeRMN3",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import numpy as np \n",
        "import torch \n",
        "import torch.nn as nn \n",
        "import torchvision \n",
        "import torchvision.transforms as transforms "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "JJPn7gxxRh1O",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x = torch.tensor(1., requires_grad=True)\n",
        "w = torch.tensor(2., requires_grad=True)\n",
        "b = torch.tensor(3., requires_grad=True)\n",
        "\n",
        "print(x, w, b)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "MMUTQeXYR9vf",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "y = w * x + b "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hJ3ySM8hSDPO",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "y.backward()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wxlQVLWeSLKB",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(x.grad)\n",
        "print(w.grad)\n",
        "print(b.grad)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5oT5XrpwSRpH",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x = torch.randn(10, 3)\n",
        "y = torch.randn(10, 2)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "i9bsn9OgSplx",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "linear = nn.Linear(3, 2)\n",
        "print('w: ', linear.weight)\n",
        "print('b: ', linear.bias)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "o2TS7eEHTERQ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "criterion = nn.MSELoss()\n",
        "optimizer = torch.optim.SGD(linear.parameters(), lr=0.01)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Loz9EOsKjtqY",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "pred = linear(x)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "GLFLZgmKjyIt",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "loss = criterion(pred, y)\n",
        "print('loss: ', loss.item())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "WyWVxHmlj5kc",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "loss.backward()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "q6FjAYcaj8CU",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(linear.weight.grad)\n",
        "print(linear.bias.grad)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "iVdok6gGkB_M",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "optimizer.step()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "ZdcnnzwnkGT9",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "pred = linear(x)\n",
        "loss = criterion(pred, y)\n",
        "print(loss.item())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "M9kG6E1Unb5c",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "x = np.array([[1, 2], [3, 4]])\n",
        "print(x)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "aIOU2pOKnlR7",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "y = torch.from_numpy(x)\n",
        "print(y)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7wfyVsYqnqSu",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "z = y.numpy()\n",
        "print(z)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "enbu21tanu9s",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "train_dataset = torchvision.datasets.CIFAR10(root='./data', train=True, transform=transforms.ToTensor(), download=True)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "CqNsc8dMoZOD",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "image, label = train_dataset[10]"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qYR4LRT2oFY9",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(image.size())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "gg-evYEpoKn1",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "print(label)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "wc3LzPvhoguv",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "train_loader = torch.utils.data.DataLoader(dataset=train_dataset, batch_size=64, shuffle=True)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "TQsADyT1ovEk",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "data_iter = iter(train_loader)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "x3XYH7Hao7Zu",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "images, labels = data_iter.next()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hyqRV2ntpAw-",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "class CustomDataset(torch.utils.data.Dataset):\n",
        "    def __init__(self):\n",
        "        pass \n",
        "\n",
        "    def __getitem__(self, index):\n",
        "        pass \n",
        "\n",
        "    def __len__(self):\n",
        "        return 0 "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "RgbQ6Orhpg6N",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "custom_dataset = CustomDataset()\n",
        "\n",
        "train_loader = torch.utils.data.DataLoader(dataset=custom_dataset, batch_size=64, shuffle=True)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "d8j1zUHtpwjO",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "resnet = torchvision.models.resnet18(pretrained=True)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "NzP1TE6up7uc",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "for param in resnet.parameters():\n",
        "    param.requires_grad = False "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "oA8nFNpbqFeG",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "resnet.fc = nn.Linear(resnet.fc.in_features, 100)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hGt87jOZqMtX",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "images = torch.randn(64, 3, 224, 224)\n",
        "outputs = resnet(images)\n",
        "print(outputs.size())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "_g0tK5NPqaUt",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "torch.save(resnet, 'model.ckpt')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "fowhLCJ2qk_e",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "model = torch.load('model.ckpt')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Uxza5hJKqyKX",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "torch.save(resnet.state_dict(), 'params.ckpt')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "tdH_dgEsq7AW",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "resnet.load_state_dict(torch.load('params.ckpt'))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Rfv0Ja1JrBc1",
        "colab_type": "text"
      },
      "source": [
        "## 7、Forward Network "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "34-hgMkM4ut0",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch \n",
        "import torch.nn as nn \n",
        "import torchvision \n",
        "import torchvision.transforms as transforms "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "uwEJBBVS4-x4",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "input_size = 784 \n",
        "hidden_size = 500 \n",
        "num_classes = 10 \n",
        "num_epochs = 5 \n",
        "batch_size = 100 \n",
        "learning_rate = 0.001 "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "nP8loGZ-7ztE",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "# MNIST dataset \n",
        "train_dataset = torchvision.datasets.MNIST(root='../../data', \n",
        "                                           train=True, \n",
        "                                           transform=transforms.ToTensor(),  \n",
        "                                           download=True)\n",
        "\n",
        "test_dataset = torchvision.datasets.MNIST(root='../../data', \n",
        "                                          train=False, \n",
        "                                          transform=transforms.ToTensor())\n",
        "\n",
        "# Data loader\n",
        "train_loader = torch.utils.data.DataLoader(dataset=train_dataset, \n",
        "                                           batch_size=batch_size, \n",
        "                                           shuffle=True)\n",
        "\n",
        "test_loader = torch.utils.data.DataLoader(dataset=test_dataset, \n",
        "                                          batch_size=batch_size, \n",
        "                                          shuffle=False)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "7e6bZW3S5OW6",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "class Net(nn.Module):\n",
        "    def __init__(self, input_size, hidden_size, num_classes):\n",
        "        super(Net, self).__init__()\n",
        "        self.fc1 = nn.Linear(input_size, hidden_size)\n",
        "        self.relu = nn.ReLU()\n",
        "        self.fc2 = nn.Linear(hidden_size, num_classes)\n",
        "\n",
        "    def forward(self, x):\n",
        "        out = self.fc1(x)\n",
        "        out = self.relu(out)\n",
        "        out = self.fc2(out)\n",
        "        return out "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Hk3ymoB75a4D",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "model = Net(input_size, hidden_size, num_classes)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "lWRAfTh56R9M",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "criterion = nn.CrossEntropyLoss()\n",
        "optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "HTXjdPAm6ebc",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "total_step = len(train_loader)\n",
        "for epoch in range(num_epochs):\n",
        "    for i, (images, labels) in enumerate(train_loader):\n",
        "        images = images.reshape(-1, 28*28)\n",
        "        labels = labels \n",
        "\n",
        "        outputs = model(images)\n",
        "        loss = criterion(outputs, labels)\n",
        "\n",
        "        optimizer.zero_grad()\n",
        "        loss.backward()\n",
        "        optimizer.step()\n",
        "\n",
        "        if (i+1) % 100 == 0:\n",
        "            print('Epoch [{}/{}], Step [{}/{}], Loss: {:.4f}'.format(epoch+1, num_epochs, i+1, total_step, loss.item()))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "EJhOV07t7ct8",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "with torch.no_grad():\n",
        "    correct = 0 \n",
        "    total = 0 \n",
        "    for images, labels in test_loader:\n",
        "        images = images.reshape(-1, 28*28)\n",
        "        labels = labels\n",
        "        outputs = model(images)\n",
        "        _, predicted = torch.max(outputs.data, 1)\n",
        "        total += labels.size(0)\n",
        "        correct += (predicted==labels).sum().item()\n",
        "\n",
        "    print('Accuracy: {} %'.format(100 * correct / total))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Wuq6oLv38wxn",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "torch.save(model.state_dict(), 'model.ckpt')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "SLzF1mZP8-jn",
        "colab_type": "text"
      },
      "source": [
        "## 8、CNN model"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5HspUN7hV-YG",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torchvision \n",
        "model = torchvision.models.alexnet(pretrained=False)\n",
        "print(model)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1svbeFL7WJcF",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torchvision \n",
        "model = torchvision.models.vgg16(pretrained=False)\n",
        "print(model)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "1YLf8P5_WTk6",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torchvision \n",
        "model = torchvision.models.inception_v3(pretrained=False)\n",
        "print(model)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "q1dQaKTGWgKy",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "model = torchvision.models.resnet18(pretrained=False)\n",
        "print(model)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "Fo1uXh60Wud7",
        "colab_type": "text"
      },
      "source": [
        "## 9、RNN model "
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "4PqyBUYUW5Gp",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "rnn = torch.nn.RNN(20, 50, 2)\n",
        "input = torch.randn(100, 32, 20)\n",
        "h_0 = torch.randn(2, 32, 50)\n",
        "output, hn = rnn(input, h_0)\n",
        "print(output.size(), hn.size())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "u1A8kv_qXZGM",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "lstm = torch.nn.LSTM(10, 20, 2)\n",
        "input = torch.randn(5, 3, 10)\n",
        "h0 = torch.randn(2, 3, 20)\n",
        "c0 = torch.randn(2, 3, 20)\n",
        "output, hn = lstm(input, (h0, c0))\n",
        "print(output.size(), hn[0].size(), hn[1].size())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "0ITSSFNTYL-r",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "rnn = torch.nn.GRU(10, 20, 2)\n",
        "input = torch.randn(5, 3, 10)\n",
        "h_0 = torch.randn(2, 3, 20)\n",
        "output, hn = rnn(input, h0)\n",
        "print(output.size(), h0.size())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Uvouy-H1YhVd",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "embedding = torch.nn.Embedding(10, 3)\n",
        "input = torch.LongTensor([[1, 2,4, 5], [4, 3, 2, 9]])\n",
        "output=embedding(input)\n",
        "print(output.size())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "id": "v0JmUiQFY1re",
        "colab_type": "text"
      },
      "source": [
        "## 10、Logistic Regression"
      ]
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "zEur71LUZyRd",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "import torch \n",
        "import torch.nn as nn \n",
        "import numpy as np "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "kAT2R6dDaIby",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "from google.colab import files"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "sMEDg1FOaMnC",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "uploaded = files.upload()"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "DGE6GYX-aaSM",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "data = np.loadtxt('german.data-numeric')"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "cOINDj2FarEL",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "n, l = data.shape\n",
        "for j in range(l-1):\n",
        "    meanVal = np.mean(data[:, j])\n",
        "    stdVal = np.std(data[:, j])\n",
        "    data[:, j] = (data[:, j] - meanVal) / stdVal"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Bg1XwS63bIMn",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "np.random.shuffle(data)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "3llm3uzEbMJF",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "train_data = data[:900, :l-1]\n",
        "train_labels = data[:900, l-1] - 1\n",
        "\n",
        "test_data = data[900:, :l-1]\n",
        "test_labels = data[900:, l-1] - 1 "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "w0C7wo7ibtQs",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "class LR(nn.Module):\n",
        "    def __init__(self):\n",
        "        super(LR, self).__init__()\n",
        "        self.fc = nn.Linear(24, 2)\n",
        "    def forward(self, x):\n",
        "        out = self.fc(x)\n",
        "        out = torch.sigmoid(out)\n",
        "        return out "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Oe57Ts5uezcP",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "def test(pred, lab):\n",
        "    t = pred.max(-1)[1] == lab \n",
        "    return torch.mean(t.float())"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "hvMVw9tt5hFQ",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "net = LR()\n",
        "criterion = nn.CrossEntropyLoss()\n",
        "optimizer = torch.optim.Adam(net.parameters())\n",
        "num_epochs = 1000 "
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qLZIDynS5x4j",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        "for epoch in range(num_epochs):\n",
        "    net.train()\n",
        "    x = torch.from_numpy(train_data).float()\n",
        "    y = torch.from_numpy(train_labels).long()\n",
        "    outputs = net(x)\n",
        "    loss = criterion(outputs, y)\n",
        "    optimizer.zero_grad()\n",
        "    loss.backward()\n",
        "    optimizer.step()\n",
        "\n",
        "    if (epoch+1) % 100 == 0:\n",
        "        net.eval()\n",
        "        test_in = torch.from_numpy(test_data).float()\n",
        "        test_l = torch.from_numpy(test_labels).long()\n",
        "        test_out = net(test_in)\n",
        "        accuracy = test(test_out, test_l)\n",
        "        print('Epoch {}, Loss: {:.4f}, Accuracy: {:.2f}'.format(epoch+1, loss.item(), accuracy))"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "6OHMWy2t7h3k",
        "colab_type": "code",
        "colab": {}
      },
      "source": [
        ""
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}